package com.example.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.chapter03.utils.DateUtil;

public class ButtonLongClickActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_long_click);

        TextView text_view = findViewById(R.id.tv_result);
        Button btn_long_click = findViewById(R.id.btn_long_click);

        btn_long_click.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String desc = String.format("%s 长按了按钮，%s", DateUtil.getNowDateTime(), ((Button) v).getText());
                text_view.setText(desc);
                return true;
            }
        });

        // 以上代码可以简化为以下代码, 使用 lambda 表达式
        btn_long_click.setOnLongClickListener(v -> {
            String desc = String.format("lambda %s 长按了按钮，%s", DateUtil.getNowDateTime(), ((Button) v).getText());
            text_view.setText(desc);
            return true;
        });


    }
}